package com.guigu.liuyunan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.liuyunan.mapper.SetmealMapper;
import com.guigu.liuyunan.mapper.SetmealcheckgroupMapper;
import com.guigu.liuyunan.pojo.LYNPageVo;
import com.guigu.liuyunan.pojo.Setmeal;
import com.guigu.liuyunan.pojo.Setmealcheckgroup;
import com.guigu.liuyunan.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper sm;

    @Override
    public PageInfo setmealInfo(LYNPageVo LYNPageVo) {
        PageHelper.startPage(LYNPageVo.getPage(), LYNPageVo.getNum());
        List<Setmeal> setmeals = sm.setmealInfo(LYNPageVo);
        return new PageInfo(setmeals);
    }

    @Override
    public int delSetmeal(Integer id) {
        sc.deleteByPrimaryKey(id);
        return sm.deleteByPrimaryKey(id);
    }


    @Autowired
    SetmealcheckgroupMapper sc;

    @Override
    public int addSetmeal(Setmeal setmeal) {
        sm.insert(setmeal);

        List<Setmealcheckgroup> list=new ArrayList<>();
        Integer[] setmealList = setmeal.getCheckitemArray();
        for (int i = 0; i < setmealList.length; i++) {
            Setmealcheckgroup mem = new Setmealcheckgroup();
            mem.setCheckgroupid(setmealList[i]);
            mem.setSetmealid(setmeal.getId());
            list.add(mem);
        }
        return  sc.insert(list);

    }

    @Override
    public int UpSetmeal(Setmeal setmeal) {
        sc.deleteByPrimaryKey(setmeal.getId());

        List<Setmealcheckgroup> list=new ArrayList<>();
        Integer[] setmealList = setmeal.getCheckitemArray();
        for (int i = 0; i < setmealList.length; i++) {
            Setmealcheckgroup mem = new Setmealcheckgroup();
            mem.setCheckgroupid(setmealList[i]);
            mem.setSetmealid(setmeal.getId());
            list.add(mem);
        }
        sc.insert(list);
        return sm.updateByPrimaryKey(setmeal);
    }
}
